package com.ruoyi.quartz.task;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.websocket.WebSocketServer;
import com.ruoyi.work.domain.*;
import com.ruoyi.work.domain.vo.LocationTrainVo;
import com.ruoyi.work.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 定时任务调度测试
 *
 * @author szm
 */
@Slf4j
@Component("locationTrainTask")
public class LocationTrainTask {
    @Autowired
    private ITbLocationTrainService locationTrainService;
    @Autowired
    private ITbEquTrainService tbEquTrainService;
    @Autowired
    private ITbLocationUserService tbLocationUserService;
    @Autowired
    private ITbWorkOrderUserService tbWorkOrderUserService;
    @Autowired
    private ITbWorkOrderService tbWorkOrderService;
    @Autowired
    private WebSocketServer webSocketServer;

    public void lastLocatinTrain(){

        List<TbEquTrain> trains = tbEquTrainService.list();
        Map<String, Object> map = new TreeMap<>();
        map.put("tarin","");
        map.put("user","");
        map.put("workOrder","");
        List<LocationTrainVo> trainVoList = new LinkedList<>();
        for (TbEquTrain train : trains){
            List<TbLocationTrain> locationTrainList = locationTrainService.lambdaQuery()
                    .eq(TbLocationTrain::getTrainNo, train.getTrainNo())
                    .orderByDesc(TbLocationTrain::getId)
                    .last("limit 1").list();
            if (CollectionUtil.isNotEmpty(locationTrainList)){
                TbLocationTrain locationTrain = locationTrainList.get(0);
                LocationTrainVo trainVo = new LocationTrainVo();
                trainVo.setLastTime(locationTrain.getLastTime());
                trainVo.setLineName(train.getLineName());
                trainVo.setLatitude(locationTrain.getLatitude());
                trainVo.setLongitude(locationTrain.getLongitude());
                trainVo.setEquNo(locationTrain.getTerminaPhone());
                trainVo.setWorkNo(locationTrain.getWorkNo());
                trainVo.setSpeed(locationTrain.getSpeed());
                trainVo.setTrainNo(locationTrain.getTrainNo());
                trainVoList.add(trainVo);
            }
        }

        map.put("tarin",trainVoList);

        List<LocationTrainVo> userVoList = new LinkedList<>();
        List<TbWorkOrderUser> userList = tbWorkOrderUserService.getWorkUserByNo();
        for (TbWorkOrderUser user : userList){
            List<TbLocationUser> locationUsers = tbLocationUserService.lambdaQuery().eq(TbLocationUser::getUserNo,user.getUserNo())
                    .orderByDesc(TbLocationUser::getId).last("limit 1").list();
            if (CollectionUtil.isNotEmpty(locationUsers)){
                TbLocationUser locationUser = locationUsers.get(0);
                LocationTrainVo userVo = new LocationTrainVo();
                userVo.setLastTime(locationUser.getCreateTime());
                userVo.setLatitude(locationUser.getLatitude());
                userVo.setLongitude(locationUser.getLongitude());
                userVo.setWorkNo(locationUser.getWorkNo());
                userVo.setUserNo(locationUser.getUserNo());
                userVoList.add(userVo);
            }
        }
        map.put("user",userVoList);

        List<TbWorkOrder> orderList = tbWorkOrderService.lambdaQuery().eq(TbWorkOrder::getStatus, "1").list();
        map.put("workOrder",orderList);

        log.info("webSocket定时返回定位数据："+JSON.toJSONString(map));
        webSocketServer.pushMessage("train_outer", JSON.toJSONString(map));
        webSocketServer.pushMessage("train_inner", JSON.toJSONString(map));

    }

}
